// pages/uploadInfo/uploadInfo.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var uploadJs = require('../../utils/upload');
var tip = require("../../utils/tip");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    font: "",
    url: "",
    weight: "",
    item: {},
    path: ""
  },

  weightInput(e) {
    this.setData({
      weight: e.detail.value
    })
  },

  //上传图片
  upload: function () {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        // console.log(tempFilePaths, 'tempFilePaths')
        if (tempFilePaths && tempFilePaths.length > 0) {
          uploadJs.upload('page', tempFilePaths).then((res) => {
            that.setData({
              url: res.data.link,
            })
            console.log(res, '上传返回的数据')
          })
        }
      }
    })
  },

  uploadSure: async function () {
    if (!this.data.weight) {
      tip.showToast('请输入装车净重', 'error', 1000)
      return
    }
    if (!this.data.url) {
      tip.showToast('请输入上传' + this.data.font + '磅单', 'error', 1000)
      return
    }

    let requestUrl = url.uploadLoadingInfo;
    let json = {
      'waybillNumber': this.data.item.waybillNumber,
      'loadingNetWeight': this.data.weight,
      'loadingPoundsSingle': this.data.url
    }
    if (this.data.font == '卸车') {
      requestUrl = url.unloadingScale
      json = {
        'waybillNumber': this.data.item.waybillNumber,
        'unloadingNetWeight': this.data.weight,
        'unloadingPoundsSingle': this.data.url
      }
    }

    let data = await commons.postJson(requestUrl, json);
    if (data.code == 200) {
      tip.showToast(data.data, 'success', 1000)
      setTimeout(() => {
        let pages = getCurrentPages()
        pages[pages.length - 2].getWaybillList()
        // if (this.data.path == 'historicalWaybill') {
        //   pages[pages.length - 2].getWaybillList()
        //   pages[pages.length - 3].getWaybillList()
        // } else if (this.data.path == 'book') {
        //   pages[pages.length - 2].getWaybillList()
        // } else {
        //   pages[pages.length - 2].getDetailsInTransit()
        // }
        wx.navigateBack({
          delta: 1,
        })
      }, 1000)
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },

  uploadLoadingInfo() {

  },

  unloadingScale() {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    let obj = {}
    if (e.obj) {
      obj = JSON.parse(e.obj)
    }
    // console.log(obj, '上传装卸货信息页')
    if (obj.type) {
      let font = obj.type == "upload" ? "装车" : "卸车"
      this.setData({
        font: font
      })
      wx.setNavigationBarTitle({
        'title': "上传" + font + "信息"
      })
    }

    if (obj.item) {
      this.setData({
        item: obj.item
      })
      if (obj.type == "upload") {
        if (obj.item.loadingPoundsSingle) {
          this.setData({
            url: obj.item.loadingPoundsSingle
          })
        }
        if (obj.item.loadingNetWeight) {
          this.setData({
            weight: obj.item.loadingNetWeight
          })
        }
      } else if (obj.type == "unload") {
        if (obj.item.unloadingPoundsSingle) {
          this.setData({
            url: obj.item.unloadingPoundsSingle
          })
        }
        if (obj.item.unloadingNetWeight) {
          this.setData({
            weight: obj.item.unloadingNetWeight
          })
        }
      }
    }

    if (obj.path) {
      this.setData({
        path: obj.path
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})